import { login, logout, getInfo, getdata, search, upEdit, homeAddress_getTableData, collect_getTableData, footprint_getTableData,search_getTableData,feedback_getTableData } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'

const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    avatar: ''
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ username: username.trim(), password: password }).then(response => {
        const { data } = response
        commit('SET_TOKEN', data.token)
        setToken(data.token)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        const { data } = response

        if (!data) {
          return reject('Verification failed, please Login again.')
        }

        const { name, avatar } = data

        commit('SET_NAME', name)
        commit('SET_AVATAR', avatar)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        removeToken() // must remove  token  first
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  },
  // user data   初始化表格
  getdata({ commit, state }) {
    // console.log(data, 'data')
    return new Promise((resolve, reject) => {
      getdata(state.data).then((res) => {
        // removeToken() // must remove  token  first
        resetRouter()
        commit('RESET_STATE')
        resolve(res)
      }).catch(error => {
        reject(error)
      })
    })
  },
  // 搜索表格
  search({ commit, state }, data) {
    console.log(data, 'data')
    return new Promise((resolve, reject) => {
      search(
        data
      ).then((res) => {
        // removeToken() // must remove  token  first
        resolve(res)
      }).catch(error => {
        reject(error)
      })
    })
  },
  // 编辑
  upEdit({ commit, state }, data) {
    console.log(data, 'data')
    return new Promise((resolve, reject) => {
      upEdit(
        data
      ).then((res) => {
        // removeToken() // must remove  token  first
        resolve(res)
      }).catch(error => {
        reject(error)
      })
    })
  },
  // 收货地址
  homeAddress_getTableData({ commit, state }, data) {
    console.log(data, 'data')
    return new Promise((resolve, reject) => {
      homeAddress_getTableData(
        data
      ).then((res) => {
        // removeToken() // must remove  token  first
        resolve(res)
      }).catch(error => {
        reject(error)
      })
    })
  },
  // 会员收藏
  collect_getTableData({ commit, state }, data) {
    console.log(data, 'data')
    return new Promise((resolve, reject) => {
      collect_getTableData(
        data
      ).then((res) => {
        // removeToken() // must remove  token  first
        resolve(res)
      }).catch(error => {
        reject(error)
      })
    })
  },
  /* 会员足迹 */
  // 初始化表格
  footprint_getTableData({ commit, state }, data) {
    console.log(data, 'data')
    return new Promise((resolve, reject) => {
      footprint_getTableData(
        data
      ).then((res) => {
        // removeToken() // must remove  token  first
        resolve(res)
      }).catch(error => {
        reject(error)
      })
    })
  },
  /* 搜索历史 */
  search_getTableData({ commit, state }, data) {
    console.log(data, 'data')
    return new Promise((resolve, reject) => {
      search_getTableData(
        data
      ).then((res) => {
        // removeToken() // must remove  token  first
        resolve(res)
      }).catch(error => {
        reject(error)
      })
    })
  },
 /* 意见反馈 */
feedback_getTableData({ commit, state }, data) {
  console.log(data, 'data')
  return new Promise((resolve, reject) => {
    feedback_getTableData(
      data
    ).then((res) => {
      // removeToken() // must remove  token  first
      resolve(res)
    }).catch(error => {
      reject(error)
    })
  })
},




}





export default {
  namespaced: true,
  state,
  mutations,
  actions
}

